﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.ExceptionManagement;
using Kiiro.Validation.Validators.Entities;

namespace Kiiro.Model.EventHandlers.ItemEventHandlers
{
    public class WorkItemEventReceiver : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
            Guid siteCollectionId = new Guid();
            string serverRelativeUrl = null;

            ActivityHistory activity = null;

            properties.ListItem[Constants.WorkFieldNames.ServerRelativeUrl] = properties.ListItem.Folder.ServerRelativeUrl;
            Work work = SPDataHelpers.MakeWork(properties.ListItem);
            properties.ListItem[Constants.WorkFieldNames.IsFromProjectPlan] = false;

            if (!work.IsFromProjectPlan)
            {
                activity = PopulateWorkHistory(work);
                if (work.Status == StatusType.Completed || work.Status == StatusType.Closed)
                    activity.EventType = EventType.Completed;
                else
                    activity.EventType = EventType.Created;
            }

            using (SPWeb web = properties.ListItem.ParentList.ParentWeb)
            {
                web.AllowUnsafeUpdates = true;
                this.DisableEventFiring();
                properties.ListItem.Update();
                this.EnableEventFiring();
                web.AllowUnsafeUpdates = false;

                siteCollectionId = web.Site.ID;
                serverRelativeUrl = web.ServerRelativeUrl;
            }

            if (activity != null)
                ActivityHistoryModel.Save(activity, siteCollectionId, serverRelativeUrl);

            base.ItemAdded(properties);
        }

        public override void ItemDeleting ( SPItemEventProperties properties )
        {
            base.ItemDeleting ( properties );
        }

        public override void ItemUpdated ( SPItemEventProperties properties )
        {
            Guid siteCollectionId = new Guid();
            string serverRelativeUrl = null;

            ActivityHistory activity = null;

            Work work = SPDataHelpers.MakeWork(properties.ListItem);
            properties.ListItem[Constants.WorkFieldNames.IsFromProjectPlan] = false;

            if (!work.IsFromProjectPlan)
            {
                activity = PopulateWorkHistory(work);
                if ( work.Status == StatusType.Completed || work.Status == StatusType.Closed )
                    activity.EventType = EventType.Completed;
                else
                    activity.EventType = EventType.Updated;                
            }

            using (SPWeb web = properties.ListItem.ParentList.ParentWeb)
            {
                web.AllowUnsafeUpdates = true;
                this.DisableEventFiring();
                properties.ListItem.Update();
                this.EnableEventFiring();
                web.AllowUnsafeUpdates = false;

                siteCollectionId = web.Site.ID;
                serverRelativeUrl = web.ServerRelativeUrl;
            }

            if (activity != null)
                ActivityHistoryModel.Save(activity, siteCollectionId, serverRelativeUrl);
            
            base.ItemUpdated(properties);
        }

        private ActivityHistory PopulateWorkHistory(Work work)
        {
            ActivityHistory activity = new ActivityHistory();

            activity.CreatedBy = work.ModifiedBy;
            activity.CreatedDate = work.ModifiedDate;
            activity.ModifiedBy = work.ModifiedBy;
            activity.ModifiedDate = work.ModifiedDate;
            activity.Title = work.Title;

            activity.ActivityItemType = work.WorkItemType == WorkType.Task ? ItemType.Task : ItemType.Issue;
            activity.ActivityReferenceUrl = work.ServerRelativeUrl;

            activity.EditType = EditType.Created;

            return activity;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/EventHandlers/ItemEventHandlers/WorkItemEventReceiver.cs $
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/